﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MySoftLab.DapFlashCore.Targets
{
    public class STM32L471 : TargetBase
    {
        private static FlashAlgo _flashAlgo = new()
        {
            LoadAddress = 0x20000000,
            Instructions =
            [
                0xE00ABE00, 0x062D780D, 0x24084068, 0xD3000040, 0x1E644058, 0x1C49D1FA, 0x2A001E52, 0x4770D1F2,
                0x8f4ff3bf, 0x48584770, 0x49586800, 0x0d000500, 0xd0001840, 0x47702001, 0x6a004855, 0x0fc00280,
                0xb5004770, 0xf7ff4602, 0x2801ffee, 0xf7ffd108, 0x2801fff3, 0x484fd104, 0xd3014282, 0xbd002001,
                0xbd002000, 0x4602b500, 0xffddf7ff, 0xd0022801, 0x0d8002d0, 0x4948bd00, 0x40080ad0, 0xd5f90311,
                0x300130ff, 0x4842bd00, 0x60814944, 0x60814944, 0x60012100, 0x61014943, 0x03c06a00, 0x4843d406,
                0x60014941, 0x60412106, 0x60814941, 0x47702000, 0x49372001, 0x614807c0, 0x47702000, 0x47702001,
                0x49384833, 0x13c16101, 0x69416141, 0x04122201, 0x61414311, 0x4a354937, 0x6011e000, 0x03db6903,
                0x2100d4fb, 0x46086141, 0xb5104770, 0xf7ff4604, 0x4603ffa8, 0xf7ff4620, 0x4925ffb5, 0x610c4c29,
                0x02d800c2, 0x43021c92, 0x6948614a, 0x04122201, 0x61484310, 0x8f4ff3bf, 0x4a244826, 0x6010e000,
                0x03db690b, 0x2000d4fb, 0x69086148, 0xd0014020, 0x2001610c, 0xb5f0bd10, 0x4d151dc9, 0x4f1908c9,
                0x612f00c9, 0x616b2300, 0xe0184c1a, 0x616b2301, 0x60036813, 0x60436853, 0x8f4ff3bf, 0xe0004b13,
                0x692e601c, 0xd4fb03f6, 0x616b2300, 0x423b692b, 0x612fd002, 0xbdf02001, 0x39083008, 0x29003208,
                0x2000d1e4, 0x0000bdf0, 0xe0042000, 0xfffffbcb, 0x40022000, 0x08080000, 0x000002ff, 0x45670123,
                0xcdef89ab, 0x0000c3fa, 0x00005555, 0x40003000, 0x00000fff, 0x0000aaaa, 0x00000000
            ],
            PcInit = 0x20000087,
            PcUnInit = 0x200000b1,
            PcProgramPage = 0x20000137,
            PcEraseSector = 0x200000eb,
            PcEraseAll = 0x200000c1,
            StaticBase = 0x20000000 + 0x00000020 + 0x00000198,
            BeginStack = 0x20000000 + 0x00000400,
            BeginData = 0x20001000,
            PageSize = 1024,
            SectorSize = 2048,
            PageBuffers = [0x20001000, 0x20001400],
            MinProgramLength = 8,
            AnalyzerSupported = true,
            AnalyzerAddress = 0x20002000
        };


        public override FlashAlgo FlashAlgo
        {
            get { return _flashAlgo; }
        }

        private static MemoryMap _memoryMap = new(
            new FlashRegion(FlashRegionName, 0x08000000, 0x100000, 0x800, 0x400, true, _flashAlgo),
            new RamRegion("sram1", 0x20000000, 0x18000),
            new RamRegion("sram2", 0x10000000, 0x8000)
            );
        public override MemoryMap MemoryMap => _memoryMap;

        public override string Name => "STM32L471";

        private static readonly Lazy<STM32L471> _instance = new Lazy<STM32L471>(() => new STM32L471());

        public static STM32L471 Instance => _instance.Value;

        private STM32L471()
        {
        }
    }
}
